package com.tyk.bookstore.back.system.util;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.function.Function;

/**
 * @author tyk
 * @description 会员模块工具类
 */
public class UserUtil {

    private final static String USER_ACCOUNT_PREFIX = "abs_";
    private final static int USER_ACCOUNT_LENGTH = 16;
    private final static int RETRY_COUNT = 5;

    /**
     * 管理员分配账号
     */
    public static String account(Function<String, Boolean> exist) {
        int cnt = RETRY_COUNT;
        while (cnt-- > 0) {
            String account = USER_ACCOUNT_PREFIX + RandomStringUtils.randomAlphanumeric(USER_ACCOUNT_LENGTH);
            if (!exist.apply(account)) return account;
        }
        return null;
    }
}
